package com.will.txj.aj.security.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.FilterConfig;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @author: wen-yi;
 * @date: 2021/12/5 11:13;
 * @Description: 使用场景：一、对请求进行字符过滤 二、对用户访问servlet请求进行会话过滤
 */
@Slf4j
@Component
public class CharacterFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        log.info("初始化CharacterFilter过滤器");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        request.setCharacterEncoding(StandardCharsets.UTF_8.toString());
        response.setCharacterEncoding(StandardCharsets.UTF_8.toString());
        if (filterChain!=null){
            filterChain.doFilter(request, response);
        }
    }

    @Override
    public void destroy() {
        log.info("销毁CharacterFilter过滤器");
    }
}
